package cn.edu.scau.vsac.service.impl;

import cn.edu.scau.vsac.dao.LoginTicketMapper;
import cn.edu.scau.vsac.dao.UserDao;
import cn.edu.scau.vsac.entity.LoginTicket;
import cn.edu.scau.vsac.entity.User;
import cn.edu.scau.vsac.service.UserService;
import cn.edu.scau.vsac.util.CommunityUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户表（user）(User)表服务实现类
 *
 * @author makejava
 * @since 2023-08-04 15:19:19
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginTicketMapper loginTicketMapper;

    @Override
    public Map<String, Object> register(User user) {
        Map<String, Object> map = new HashMap<>();
        if (user == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }
        if (user.getUsername().isBlank()) {
            map.put("usernameMsg", "账号不能为空!");
            return map;
        }
        if (user.getPassword().isBlank()) {
            map.put("passwordMsg", "密码不能为空!");
            return map;
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        List<User> users = userDao.selectList(queryWrapper);
        if (users.size() != 0) {
            map.put("usernameMsg", "该账号已存在!");
            return map;
        }
        user.setRole(1);
        user.setSex("男");
        user.setInfo("Default");
        userDao.insert(user);

        return map;
    }

    @Override
    public Map<String, Object> login(String username, String password, int expiredSeconds) {
        Map<String, Object> map = new HashMap<>();

        // 空值处理
        if (username.isBlank()) {
            map.put("usernameMsg", "账号不能为空!");
            return map;
        }
        if (password.isBlank()) {
            map.put("passwordMsg", "密码不能为空!");
            return map;
        }

        // 验证账号
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        List<User> users = userDao.selectList(queryWrapper);
        if (users.size() == 0) {
            map.put("usernameMsg", "该账号不存在!");
            return map;
        }

        User user = users.get(0);

        // 验证密码
        if (!user.getPassword().equals(password)) {
            map.put("passwordMsg", "密码不正确!");
            return map;
        }

        // 生成登录凭证
        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setUserId(user.getUserId());
        loginTicket.setTicket(CommunityUtil.generateUUID());
        loginTicket.setStatus(0);
        loginTicket.setExpired(new Date(System.currentTimeMillis() + expiredSeconds * 1000));
        loginTicketMapper.insertLoginTicket(loginTicket);

        map.put("ticket", loginTicket.getTicket());
        return map;
    }

    @Override
    public void logout(String ticket) {
        loginTicketMapper.updateStatus(ticket, 1);
    }


    @Override
    public LoginTicket findLoginTicket(String ticket) {
        return loginTicketMapper.selectByTicket(ticket);
    }
}

